﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BattleCore.Events;
using PsyModules;

namespace Battle
{
    public class PublicStore
    {
        private StoreInfo BzStore = new StoreInfo();
        private ShortChat msg = new ShortChat();
        private List<Customer> _Customers = new List<Customer>();

        public void LoadSettings()
        {
            BzStore.StoreName = "BZ Mart";
            BzStore.StoreOpen = true;
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Brick, 7));
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Burst, 25));
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Decoy, 5));
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Portal, 10));
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Repel, 20));
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Rocket, 12));
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Thor, 20));
            BzStore.Inventory.Add(new StoreItem(StoreItemType.Super, 100));
        }
        public void GetInventory(string PlayerName, Queue<EventArgs> q)
        {
            q.Enqueue(msg.pm(PlayerName, " _________~~[  BZ - MART  ]~~_______"));
            q.Enqueue(msg.pm(PlayerName, "/         Located at P14 safe.      \\"));
            q.Enqueue(msg.pm(PlayerName, "|Turn on radar in safe to view menu |"));
            q.Enqueue(msg.pm(PlayerName, "+-----------------------------------+"));
            q.Enqueue(msg.pm(PlayerName, "|  ITEM               |  PRICE      |"));
            q.Enqueue(msg.pm(PlayerName, "+---------------------+-------------+"));

            BzStore.Inventory.ForEach(delegate(StoreItem s)
            {
                q.Enqueue(msg.pm(PlayerName, "|  " + s.Type.ToString().PadRight(19, ' ') + "|  $" + String.Format("{0:0.00}", s.Price).PadLeft(8,' ') + "  |"));
            });

            q.Enqueue(msg.pm(PlayerName, "+---------------------+-------------+"));
        }
        public void InsideStore(PlayerPositionEvent p, Queue<EventArgs> q)
        {
            Customer customer = GetCustomer(p.PlayerName);

            if (p.ShipState.XRadarActive)
            {
                if (!customer.MenuOn)
                {
                    customer.MenuOn = true;
                    //reply = GetInventory(p.PlayerName);
                    q.Enqueue(msg.pm(p.PlayerName, "*objset +95,"));
                }
            }
            else if (customer.MenuOn)
            {
                customer.MenuOn = false;
                q.Enqueue(msg.pm(p.PlayerName, "*objset -95,"));
            }
        }
        public void StoreExit(PlayerPositionEvent p, Queue<EventArgs> q)
        {
            bool OnList = _Customers.Exists(delegate(Customer c) { return c.CustomerName == p.PlayerName; });

            if (OnList)
            {
                _Customers.ForEach(delegate(Customer c2)
                {
                    if (p.PlayerName == c2.CustomerName)
                    {
                        if (c2.MenuOn)
                            q.Enqueue(msg.pm(p.PlayerName, "*objset -95,"));

                        _Customers.Remove(c2);
                    }
                });
            }
        }
        private Customer GetCustomer(string PlayerName)
        {
            Customer cust = new Customer(PlayerName);
            bool found = false;
            _Customers.ForEach(delegate(Customer c)
            {
                if (PlayerName == c.CustomerName)
                {
                    cust = c;
                    found = true;
                }
            });

            if (!found)
            {
                _Customers.Add(cust);

                _Customers.ForEach(delegate(Customer c)
                {
                    if (PlayerName == c.CustomerName)
                        cust = c;
                });
            }

            return cust;
        }
        public bool IsInStore(string PlayerName)
        {
            return _Customers.Exists(delegate(Customer c) { return c.CustomerName == PlayerName; });
        }
        public void MakePurchase(PublicPlayer Player, string ItemName, ScoreBoard BZ_Bux_Lvz, Queue<EventArgs> q)
        {
            bool found = false;

            BzStore.Inventory.ForEach(delegate(StoreItem s)
            {
                if (s.Type.ToString().ToLower() == ItemName)
                {
                    if (Player.BZBux < s.Price)
                    {
                        found = true;
                        q.Enqueue(msg.pm(Player.PlayerName, "You broke bastard, you do not have enough money. Leave my store at once!"));
                    }
                    else
                    {
                        found = true;
                        int oldscore = (int)(Player.BZBux * 100);
                        Player.BZBux -= s.Price;
                        q.Enqueue(msg.pm(Player.PlayerName, "*prize #" + (int)s.Type));
                        q.Enqueue(msg.pm(Player.PlayerName, "You have purchased a [ " + s.Type.ToString() + " ] and been deducted: " + String.Format("{0:0.00}", s.Price) + " from your account."));

                        q.Enqueue(msg.pm(Player.PlayerName,BZ_Bux_Lvz.UpdateScoreCommand(oldscore, (int)(Player.BZBux * 100))));
                    }
                }
            });

            if (!found)
                q.Enqueue(msg.pm(Player.PlayerName,"Im sorry. I do not believe we carry any [ " + ItemName + " ]s"));
        }
    }
}
